//
//  HLMineHeaderView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLMineHeaderView.h"
#import "HLFriendViewController.h"
#import "HLWebViewController.h"
#import "HLVIPRequest.h"

@interface HLMineHeaderView ()
@property (nonatomic, strong) UITapGestureRecognizer *infoTapGesture;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation HLMineHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorClear;
        self.contentView.backgroundColor = UIColorClear;
        [self setupView];
    }
    return self;
}


- (void)setupView {
    self.infoView = [[HLMineHeaderInfoView alloc] init];
    [self.contentView addSubview:_infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
    }];
    
    if (![HLRunTime shareInstance].isFood){
        self.vipView = [[HLMineHeaderVipView alloc] init];
        [self.contentView addSubview:_vipView];
        [_vipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.top.equalTo(_infoView.mas_bottom).offset(12);
        }];
        
        self.incomeView = [[HLMineHeaderIncomeView alloc] init];
        [self.contentView addSubview:_incomeView];
        [_incomeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.top.equalTo(_vipView.mas_bottom).offset(8);
            
        }];
    }
    
    self.bottomView = [[UIView alloc] init];
    _bottomView.backgroundColor = UIColorWhite;
    _bottomView.layer.masksToBounds = YES;
    _bottomView.layer.cornerRadius = 8;
    [self.contentView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        if (![HLRunTime shareInstance].isFood){
            make.top.equalTo(_incomeView.mas_bottom).offset(8);
        } else {
            make.top.equalTo(_infoView.mas_bottom).offset(18);
        }
        make.height.mas_equalTo(16);
        make.bottom.mas_equalTo(8);
    }];
}

- (void)setModel:(HLUserInfoModel *)model
{
    _model = model;
    
    [_infoView.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar_male_placeholder"]];
    _infoView.nicknameLabel.text = [HLRunTime shareInstance].isFood ? model.nickName :[NSString stringWithFormat:@"%@·%@",model.nickName, @"男"];
    [_infoView.idView setTitle:[NSString stringWithFormat:@"ID: %@", model.userYueId] forState:UIControlStateNormal];
    _infoView.signLabel.text = model.selfSign;
    _infoView.followView.numLabel.text = model.myFollowCount;
    _infoView.fansView.numLabel.text = model.myFansCount;
    _infoView.friendView.numLabel.text = model.myFriendCount;
    _infoView.friendView.numLabel.text = model.myFriendCount;
    _vipView.openButton.hidden = ([model.vipSurplusDays intValue] > 0);
    if ([model.vipSurplusDays intValue] > 0){
        NSDate *date = [NSDate br_dateFromString:model.vipEndTime dateFormat:@"yyyy-MM-dd HH:mm:ss"];
        _vipView.titleLabel.text = [NSString stringWithFormat:@"您的会员将于%@过期", [NSDate br_stringFromDate:date dateFormat:@"yyyy-MM-dd"]];
    } else {
        _vipView.titleLabel.text = @"开通可畅享会员权益";
    }
    
    [_infoView.labelsView qmui_removeAllSubviews];
    [_infoView.labelsView addSubview:_infoView.vipView];
}

+ (NSString *)identifyCell
{
    return NSStringFromClass([self class]);
}

@end

@implementation HLMineHeaderInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    
    self.contentView = [[UIView alloc] init];
    _contentView.backgroundColor = UIColorWhite;
    [self addSubview:_contentView];
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.image = [UIImage imageNamed:@"avatar_male_placeholder"];
    _avatarView.contentMode = UIViewContentModeScaleAspectFit;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 42;
    _avatarView.layer.borderWidth = 1;
    _avatarView.layer.borderColor = UIColorWhite.CGColor;
    _avatarView.userInteractionEnabled = YES;
    [self addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(84);
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(-20);
    }];
    
    self.avatarTap = [[UITapGestureRecognizer alloc] init];
    [_avatarView addGestureRecognizer:_avatarTap];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(_avatarView).offset(26);
    }];
    
    self.previewButton = [[QMUIButton alloc] init];
    [_previewButton setImage:UIImageMake(@"mine_preview_icon") forState:UIControlStateNormal];
    [self addSubview:_previewButton];
    [_previewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_avatarView).offset(-10);
        make.bottom.equalTo(_avatarView);
    }];
    
    
    self.toolView = [[UIStackView alloc] init];
    _toolView.axis = UILayoutConstraintAxisHorizontal;
    _toolView.distribution = UIStackViewDistributionEqualSpacing;
    _toolView.alignment = UIStackViewAlignmentLeading;
    [_contentView addSubview:_toolView];
    [_toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-28);
        make.left.equalTo(_avatarView.mas_right).offset(90);
    }];
    
    self.followView = [[HLMineHeaderToolsItemView alloc] init];
    _followView.nameLabel.text = @"关注";
    [_toolView addArrangedSubview:_followView];
    [_followView.itemTapGesture addTarget:self action:@selector(followAction)];
   
    self.friendView = [[HLMineHeaderToolsItemView alloc] init];
    _friendView.nameLabel.text = @"好友";
    [_toolView addArrangedSubview:_friendView];
    [_friendView.itemTapGesture addTarget:self action:@selector(friendAction)];
    
    self.fansView = [[HLMineHeaderToolsItemView alloc] init];
    _fansView.nameLabel.text = @"粉丝";
    [_toolView addArrangedSubview:_fansView];
    [_fansView.itemTapGesture addTarget:self action:@selector(fansAction)];
    
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.font = UIFontMediumMake(14);
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"喝点拿铁咖啡";
    [_contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView);
        make.top.equalTo(_avatarView.mas_bottom).offset(12);
        make.width.mas_lessThanOrEqualTo((SCREEN_WIDTH * 0.36));
    }];
    
    self.idView = [HLUIHelper iconIDViewWithId:@"1234"];
    [_contentView addSubview:_idView];
    [_idView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.top.equalTo(_nicknameLabel.mas_bottom).offset(6);
    }];
    
    self.idCopyButton = [[QMUIButton alloc] init];
    [_idCopyButton setImage:UIImageMake(@"user_detail_copy") forState:UIControlStateNormal];
    [_idCopyButton addTarget:self action:@selector(copyID) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_idCopyButton];
    [_idCopyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_idView);
        make.left.equalTo(_idView.mas_right).offset(4);
    }];
    
    self.signLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMake(12) textColor:HLTextColor1];
    _signLabel.text = @"想不等于做，做不等于做到，做到不等于得到，更不等于成功";
    _signLabel.numberOfLines = 0;
    _signLabel.qmui_lineHeight = 16;
    [_contentView addSubview:_signLabel];
    [_signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.bottom.mas_equalTo(-12);
        make.top.equalTo(_idView.mas_bottom).offset(8);
    }];
    self.vipView = [[QMUIButton alloc] init];
    [_vipView setImage:UIImageMake(@"mine_header_vip") forState:UIControlStateNormal];
    
    self.labelsView = [[QMUIFloatLayoutView alloc] init];
    _labelsView.itemMargins = UIEdgeInsetsMake(0, 1, 0, 1);
    _labelsView.minimumItemSize = CGSizeMake(18, 18);// 以2个字的按钮作为最小宽度
    [self addSubview:_labelsView];
    [_labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel.mas_right).offset(4);
        make.right.mas_equalTo(-6);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(_nicknameLabel);
    }];
}


- (void)copyID
{
    [[UIPasteboard generalPasteboard] setString:_idView.titleLabel.text];
    [QMUITips showWithText:@"已经复制用户ID"];
}


- (void)fansAction
{
    HLFriendViewController *friendListVC = [[HLFriendViewController alloc] initWithListType:HLFriendTypeFans user:HLRunTime.shareInstance.mine];
    [self.qmui_viewController.navigationController pushViewController:friendListVC animated:YES];
}

- (void)friendAction
{
    HLFriendViewController *friendListVC = [[HLFriendViewController alloc] initWithListType:HLFriendTypeMine user:HLRunTime.shareInstance.mine];
    [self.qmui_viewController.navigationController pushViewController:friendListVC animated:YES];
}

- (void)followAction
{
    HLFriendViewController *friendListVC = [[HLFriendViewController alloc] initWithListType:HLFriendTypeAttention user:HLRunTime.shareInstance.mine];
    [self.qmui_viewController.navigationController pushViewController:friendListVC animated:YES];
}

@end


@implementation HLMineHeaderToolsItemView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.avatarView = [[UIImageView alloc] init];
        _avatarView.image = UIImageMake(@"mine_who_looking");
        _avatarView.hidden = YES;
        [self addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(10);
        }];
        
        self.avatarsView = [[QMUIGridView alloc] init];
        _avatarsView.columnCount = 3;
        _avatarsView.rowHeight = 20;
        _avatarsView.separatorWidth = -6;
        _avatarsView.separatorColor = UIColorWhite;
        _avatarsView.separatorDashed = NO;
        _avatarsView.hidden = YES;
        [self addSubview:_avatarsView];
        [_avatarsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(44, 20));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(10);
        }];
        
        self.numLabel = [[UILabel alloc] init];
        _numLabel.textColor = UIColorBlack;
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.font = UIFontMediumMake(20);
        _numLabel.text = @"0";
        [self addSubview:_numLabel];
        
        [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(10);
        }];
        
        self.nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = HLTextColor3;
        _nameLabel.font = UIFontMake(11);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.text = @"关注";
        [self addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(_numLabel.mas_bottom).offset(4);
            make.bottom.mas_equalTo(-10);
        }];
        
        self.tipLabel = [[UILabel alloc] init];
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.backgroundColor = [UIColor qmui_colorWithHexString:@"0xFC4B69"];
        _tipLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.text = @"0";
        _tipLabel.layer.masksToBounds = YES;
        _tipLabel.layer.cornerRadius = 8;
        _tipLabel.hidden = YES;
        [self addSubview:_tipLabel];
        [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(24, 16));
            make.bottom.equalTo(_numLabel.mas_top).offset(2);
            make.left.equalTo(_numLabel.mas_right).offset(2);
        }];
        
        self.itemTapGesture = [[UITapGestureRecognizer alloc] init];
        [self addGestureRecognizer:self.itemTapGesture];
        
    }
    return self;
}
@end


@implementation HLMineHeaderIncomeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.backView = [[UIImageView alloc] initWithImage:UIImageMake(@"mine_income_background")];
    _backView.contentMode = UIViewContentModeScaleAspectFill;
    _backView.layer.masksToBounds = YES;
    [self addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.iconView = [[UIImageView alloc] initWithImage:UIImageMake(@"mine_income_icon")];
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.font = UIFontMediumMake(15);
    _titleLabel.textColor = HLTextColor1;
    _titleLabel.text = @"金币余额";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(4);
        make.centerY.equalTo(_iconView);
    }];
    
    
    self.amountLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(26) textColor:HLAideColor5];
    _amountLabel.text = @"27366";
    [self addSubview:_amountLabel];
    [_amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.bottom.mas_equalTo(-18);
    }];
    
    self.rechargeButton = [[QMUIButton alloc] init];
    [_rechargeButton setImage:UIImageMake(@"mine_income_arrow") forState:UIControlStateNormal];
    [_rechargeButton setTitle:@"去充值" forState:UIControlStateNormal];
    [_rechargeButton setTitleColor:[UIColor qmui_colorWithHexString:@"#F48B01"] forState:UIControlStateNormal];
    [_rechargeButton addTarget:self action:@selector(incomeAction) forControlEvents:UIControlEventTouchUpInside];
    _rechargeButton.titleLabel.font = UIFontMediumMake(13);
    _rechargeButton.imagePosition = QMUIButtonImagePositionRight;
    _rechargeButton.spacingBetweenImageAndTitle = 3;
    [self addSubview:_rechargeButton];
    [_rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_amountLabel.mas_right).offset(8);
        make.bottom.equalTo(_amountLabel).offset(-8);
    }];
    
    self.adView = [[UIImageView alloc] initWithImage:UIImageMake(@"mine_income_ad")];
    [self addSubview:_adView];
    [_adView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.right.mas_equalTo(0);
    }];
}

- (void)incomeAction
{
    [HLUIHelper h5WithURL:HLH5IncomeUrl controller:self.qmui_viewController];
}

- (void)setModel:(HLBalanceModel *)model
{
    _model = model;
}

@end


@implementation HLMineHeaderVipView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 8;
    self.backgroundColor = [UIColor qmui_colorWithHexString:@"#15041F"];
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(vipAction)];
    [self addGestureRecognizer:_tapGesture];
    
    self.iconView = [[UIImageView alloc] init];
    _iconView.image = UIImageMake(@"mine_vip_icon");
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(28, 12));
        make.top.mas_equalTo(22);
        make.bottom.mas_equalTo(-22);
    }];
    
    self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:[UIColorWhite colorWithAlphaComponent:0.7]];
    _titleLabel.text = @"开通可畅享会员权益";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.equalTo(_iconView.mas_right).offset(14);
    }];
    
    self.openButton = [[QMUIButton alloc] init];
    [_openButton setTitle:@"立即开通" forState:UIControlStateNormal];
    [_openButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_openButton.titleLabel setFont:UIFontMediumMake(12)];
    [_openButton setContentEdgeInsets:UIEdgeInsetsMake(6, 10, 6, 10)];
    _openButton.cornerRadius = 14;
    [_openButton  setBackgroundImage:[UIImage qmui_imageWithGradientColors:@[[UIColor qmui_colorWithHexString:@"#FFEEC7"], [UIColor qmui_colorWithHexString:@"#E5B861"]] type:QMUIImageGradientTypeHorizontal locations:@[@(0), @(1)] size:CGSizeMake(70, 30) cornerRadiusArray:@[@14, @14, @14, @14]] forState:UIControlStateNormal];
    [_openButton addTarget:self action:@selector(vipAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_openButton];
    [_openButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];
}

- (void)vipAction
{
    HLWebViewController *webVC = [[HLWebViewController alloc] initWithUrl:[HLHelper addTokenParamWithBaesURL:HLH5MemberCenterUrl] hiddenNav:NO];
    webVC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"记录" style:UIBarButtonItemStylePlain target:self action:@selector(recordAction)];
    [self.qmui_viewController.navigationController pushViewController:webVC animated:YES];
}

- (void)recordAction
{
    [HLUIHelper h5WithURL:HLH5MemberCenterRecordUrl controller:self.qmui_viewController];
}
@end
